//获取首页关注
var express = require("express");
var router = express.Router();

let follow = require('../model/follow');
let article = require("../model/article")
let resource = require("../model/resource")
let problem = require("../model/problem")

router.post("/api/v1/getAttentionArtile", async (req, res) => {
  console.log(req.body);
  let userid = req.body.userid
  let skip = req.body.skip
  let limit = req.body.limit
  let followData = await follow.find({
    userid: userid
  })
  let followUser = followData.map(item => {
    return item.followid
  })
  console.log(followUser);

  let articleData = await article.find({
    userid: {
      "$in": followUser
    },
    audit: true
  }).sort({
    release_time: -1
  })

  let resourceData = await resource.find({
    userid: {
      "$in": followUser
    },
    audit: true
  }).sort({
    release_time: -1
  })

  let problemData = await problem.find({
    userid: {
      "$in": followUser
    },
    audit: true
  }).sort({
    release_time: -1
  })

  let searchData = articleData.concat(resourceData, problemData)

  searchData.sort((a, b) => {
    return Date.parse(b.release_time) - Date.parse(a.release_time)
  })
  let nmu = searchData.length
  let sendData = searchData.splice(skip, limit)

  res.json({
    code: 200,
    data: sendData,
    count: nmu
  });


})

// 导出模块（在 app.js 中引入）
module.exports = router;